<template>
<div class="user">
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-form-item label=""
          ><el-input
            v-model="formInline.nickName"
            :placeholder="$t('plus.input_user_info1')"
          ></el-input
        ></el-form-item>
        <el-form-item
          ><el-button type="primary" @click="onSubmit"
            >{{$t('plus.search')}}</el-button
          ></el-form-item
        >
        <el-form-item
          ><el-button size="small" type="primary" icon="Plus" @click="addClick"
            >{{$t('plus.add_new')}}</el-button
          ></el-form-item
        >
        <!-- <el-form-item><el-button size="small" type="primary" icon="Plus" @click="addClick" v-auth="'/plus/agent/user/add'">新增</el-button></el-form-item> -->
      </el-form>
    </div>

    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table
          size="small"
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column
            prop="userId"
            :label="$t('plus.user_id')"
            width="70"
          ></el-table-column>
          <el-table-column prop="nickName" :label="$t('plus.wechat_avatar')" width="70">
            <template #default="scope">
              <img
                class="radius"
                v-img-url="scope.row.avatarUrl"
                width="30"
                height="30"
              />
            </template>
          </el-table-column>
          <el-table-column prop="nickName" :label="$t('plus.wechat_nickname')"></el-table-column>
          <el-table-column prop="gradeName" :label="$t('plus.distribution_level1')"></el-table-column>
          <el-table-column prop="realName" :label="$t('plus.real_name')"></el-table-column>
          <el-table-column prop="mobile" :label="$t('plus.phone')"></el-table-column>
          <el-table-column prop="totalMoney" :label="$t('plus.total_commission')">
            <template #default="scope">
              <span class="orange">{{scope.row.totalMoney}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="money" :label="$t('plus.withdrawable_commission1')">
            <template #default="scope">
              <span class="orange">{{scope.row.money}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="referee.nickName"
            :label="$t('plus.referrer1')"
          ></el-table-column>
          <el-table-column prop="firstNum" :label="$t('plus.downline_users')">
            <template #default="scope">
              <p>
                <span class="gray9">{{$t('plus.level_1')}}</span> {{scope.row.firstNum}}

              </p>
              <p v-if="basicSetting.level >= 2">
                <span class="gray9">{{$t('plus.level_2')}}</span> {{scope.row.secondNum}}

              </p>
              <p v-if="basicSetting.level == 3">
                <span class="gray9">{{$t('plus.level_3')}}</span> {{scope.row.thirdNum}}

              </p>
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            :label="$t('plus.join_time')"
            width="140"
          ></el-table-column>
          <el-table-column fixed="right" :label="$t('plus.action')" width="210">
            <template #default="scope">
              <div>
                <el-button
                  @click="saleClick(scope.row)"
                  type="text"
                  size="small"
                  v-auth="'/plus/agent/order/index'"
                  >{{$t('plus.orders')}}</el-button
                >
                <el-button
                  @click="detailedClick(scope.row)"
                  type="text"
                  size="small"
                  v-auth="'/plus/agent/cash/index'"
                  >{{$t('plus.withdraw')}}</el-button
                >
                <el-button
                  @click="openSubUser(scope.row)"
                  type="text"
                  size="small"
                  v-auth="'/plus/agent/user/fans'"
                  >{{$t('plus.subordinate')}}</el-button
                >
                <el-button
                  @click="EditClick(scope.row)"
                  type="text"
                  size="small"
                  v-auth="'/plus/agent/user/edit'"
                  >{{$t('plus.modify')}}</el-button
                >
                <el-button
                  @click="delClick(scope.row)"
                  type="text"
                  size="small"
                  v-auth="'/plus/agent/user/delete'"
                  >{{$t('plus.delete')}}</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>

    <!--查看下级用户-->
    <SubUser
      :open_dialog="open_dialog"
      :userModel="userModel"
      @close="closeFunc"
    ></SubUser>

    <!--编辑-->
    <Edit
      :open_edit="open_edit"
      :userModel="userModel"
      @close="closeEditFunc"
    ></Edit>

    <!--新增-->
    <Add :open_add="open_add" @close="closeAddFunc"></Add>
  </div>
</template>

<script>
import { deepClone } from "@/utils/base.js";
import PlusApi from "@/api/plus.js";
import SubUser from "./dialog/SubUser.vue";
import Edit from "./dialog/Edit.vue";
import Add from "./dialog/Add.vue";
export default {
  components: {
    SubUser,
    Edit,
    Add
  },
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*搜索对象*/
      formInline: {
        nickName: ""
      },
      /*是否打开弹窗*/
      open_dialog: false,
      /*基础设置当前用户的信息*/
      basicSetting: [],
      /*选中的用户*/
      userModel: {},
      /*是否打开修改*/
      open_edit: false,
      open_add: false
    };
  },
  created() {
    /*获取列表*/
    this.getData();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getData();
    },
    /*获取数据*/
    getData() {
      let self = this;
      PlusApi.agentList({
        pageIndex: self.curPage,
        pageSize: self.pageSize,
        search: self.formInline.nickName
      }, true).then(res => {
        self.loading = false;
        self.tableData = res.data.list.records;
        self.totalDataNumber = res.data.list.total;
        self.basicSetting = res.data.basic;
      }).catch(error => {
        self.loading = false;
      });
    },
    //搜索
    onSubmit() {
      this.curPage = 1;
      this.getData();
      console.log(this.getData);
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getData();
    },
    /*分销订单*/
    saleClick(item) {
      this.$router.push({
        path: "/plus/agent/index",
        query: {
          type: "order",
          userId: item.userId
        }
      });
      this.$emit("chang-active", "order");
    },
    /*提现明细*/
    detailedClick(item) {
      this.$router.push({
        path: "/plus/agent/index",
        query: {
          type: "cash",
          userId: item.userId
        }
      });
      this.$emit("chang-active", "cash");
    },
    /*删除分销商*/
    delClick(row) {
      let self = this;
      ElMessageBox.confirm($t("plus.delete_confirm"), $t("plus.prompt"), {
        confirmButtonText: $t("plus.confirm_text"),
        cancelButtonText: $t("plus.cancel_text"),
        type: "warning"
      }).then(() => {
        self.loading = true;
        PlusApi.deleteagent({
          userId: row.userId
        }, true).then(data => {
          self.loading = false;
          ElMessage({
            message: data.msg,
            type: "success"
          });
          self.getData();
        }).catch(error => {
          self.loading = false;
        });
      }).catch(() => {
        self.loading = false;
      });
    },
    /*打开下级用户弹窗*/
    openSubUser(e) {
      this.userModel = e;
      this.open_dialog = true;
    },
    /*关闭下级用户弹窗*/
    closeFunc() {
      this.open_dialog = false;
    },
    /*打开编辑用户弹窗*/
    EditClick(e) {
      this.userModel = deepClone(e);
      this.open_edit = true;
    },
    /*关闭编辑用户弹窗*/
    closeEditFunc(e) {
      this.open_edit = false;
      if (e && e.type == "success") {
        this.getData();
      }
    },
    addClick() {
      this.open_add = true;
    },
    closeAddFunc(e) {
      this.open_add = false;
      if (e.type == "success") {
        this.getData();
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.el-button {
  margin-left: 0;
  margin-right: 10px;
}
</style>

